package com.jpyl.jpMedicalTreatment.view.fragment.common;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.model.ErrorMessageModel;
import com.jpyl.jpMedicalTreatment.model.ProductsMessageModel;
import com.jpyl.jpMedicalTreatment.model.ResponseModel;
import com.jpyl.jpMedicalTreatment.network.Network;
import com.jpyl.jpMedicalTreatment.network.api.ApiError;
import com.jpyl.jpMedicalTreatment.network.api.RequestApi;
import com.jpyl.jpMedicalTreatment.view.activity.product.ProductDetailsActivity;
import com.jpyl.jpMedicalTreatment.view.adapter.ProductAdapter;
import com.jpyl.jpMedicalTreatment.view.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 产品主界面
 * Created by 521546564465 on 2018/11/5.
 */

@SuppressLint("ValidFragment")
public class ProductFragment extends BaseFragment {

    ProductAdapter adapter;
    @BindView(R.id.gv_product)
    GridView gvProduct;
    @BindView(R.id.tv_product_noData)
    TextView tvProductNoData;
    Unbinder unbinder;
    final RequestApi publicApi = Network.getRequestApi();
    List<ProductsMessageModel.ProductsModel> products = new ArrayList<>();

    public ProductFragment(Context context) {
        // TODO Auto-generated constructor stub
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.product_fragment_layout;
    }

    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        adapter = new ProductAdapter(getActivity(), products);
        gvProduct.setAdapter(adapter);

        gvProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                startActivity(new Intent(getContext(), ProductDetailsActivity.class).putExtra
                        ("name",products.get(i)));
            }
        });
        jpylGetProducts();
    }


    private void jpylGetProducts() {
        final JsonObject body = new JsonObject();
        body.addProperty("rows", "10");
        body.addProperty("page", "1");

        publicApi.jpylGetProducts(body.toString()).map(new Func1<ResponseModel<ProductsMessageModel>, ProductsMessageModel>() {
            @Override
            public ProductsMessageModel call(ResponseModel<ProductsMessageModel> responseModel) {
                if (responseModel.getCode() != 0) {
                    ErrorMessageModel messageModel = new ErrorMessageModel(responseModel.getMsg());
                    throw new ApiError(messageModel);
                }
                return responseModel.getData();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ProductsMessageModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (e instanceof ApiError) {
                            ApiError error = (ApiError) e;
                        } else {
                        }
                    }

                    @Override
                    public void onNext(ProductsMessageModel model) {

                        products.addAll(model.getProducts());
                        adapter.notifyDataSetChanged();


                    }
                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
